package com.example.datatrans.controller;

import com.example.datatrans.bean.ResponseResult;
import com.example.datatrans.config.ConfigValue;
import com.example.datatrans.service.IFileSourceService;
import com.example.datatrans.vo.FileSourceVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: cs
 * @date: 2024-10-29 11:37:56
 * @since: 1.0
 */
@RestController
@RequestMapping("/filesource")
public class FileSourceController {

    @Resource
    private ConfigValue configValue;

    @Resource
    private IFileSourceService fileSourceService;

    @GetMapping("/getFileSourceStorePath")
    public ResponseResult<String> getFileSourceStorePath() {
        return ResponseResult.success(configValue.getFileSourceStorePath());
    }

    @GetMapping("/test")
    public ResponseResult<String> test() {
        return ResponseResult.success("test success");
    }


    @GetMapping("/store")
    public ResponseResult<List<FileSourceVO>> store() {
        List<FileSourceVO> fileSourceVOList = fileSourceService.storeAllFile();
        if (!fileSourceVOList.isEmpty()) {
            return ResponseResult.success(fileSourceVOList);
        } else {
            return ResponseResult.fail("备份失败!");
        }
    }


    /**
     * 从本地磁盘中读取备份的资源文件
     * @return
     */
    @GetMapping("/listFileFromDisk")
    public ResponseResult<List<FileSourceVO>> listFileFromDisk() {
        List<FileSourceVO> storedFiles = fileSourceService.findStoredFiles(configValue.getFileSourceStorePath());
        return ResponseResult.success(storedFiles);
    }


    @GetMapping("/backup")
    public ResponseResult<Boolean> backup() {
        boolean backup = fileSourceService.backup();
        return ResponseResult.success("转存入库成功", backup);
    }

}
